/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.appstreet.service.impl;

import com.opgea.appstreet.dao.CategoryDAO;
import com.opgea.appstreet.dao.CompanyDAO;
import com.opgea.appstreet.dao.CompanyLoginDAO;
import com.opgea.appstreet.domain.entities.Category;
import com.opgea.appstreet.domain.entities.Company;
import com.opgea.appstreet.domain.entities.CompanyLogin;
import com.opgea.appstreet.service.CompanyService;
import com.opgea.appstreet.web.dto.CompanyDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Ramesh
 */
@Service
public class CompanyServiceImpl implements CompanyService{

    
    @Autowired
    private CompanyDAO companyDAO;
    @Autowired
    private CategoryDAO categoryDAO;
    @Autowired
    private CompanyLoginDAO companyLoginDAO;
    
    @Override
    public CompanyDTO create(CompanyDTO companyDTO) {
        
        Category category = categoryDAO.find(companyDTO.getCategoryId());
        
        
        Company company = new Company();
        company.setName(companyDTO.getName());
        company.setEmail(companyDTO.getEmailId());
        //company.setContacts(null);
        company.setWebsite(companyDTO.getWebsite());
        company.setCategory(category);
        companyDAO.create(company);
        
        CompanyLogin companyLogin = new CompanyLogin();
        companyLogin.setLoginId(companyDTO.getEmailId());
        companyLogin.setPassword(String.valueOf(System.currentTimeMillis()));
        companyLogin.setCompany(company);
        companyLogin.setIsActive(true);
        
        companyLoginDAO.create(companyLogin);
        
        return companyDTO;        
    }
    
}
